package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.GlideUtils;
import com.egg.ylt.pojo.CollentShopEntity;
import com.egg.ylt.pojo.HomePageEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * sichaoqi on 2018/12/14
 */
public class ADA_CollectShopRecycler extends RecyclerView.Adapter<ADA_CollectShopRecycler.ShopViewHolder> {


    private Context mContext;
    private LayoutInflater mInflater;
    private List<CollentShopEntity.ListBean> mList;
    private OnItemClickListener mItemListener;

    public ADA_CollectShopRecycler(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
        mList = new ArrayList<>();
    }

    public void addList(List<CollentShopEntity.ListBean> list) {
        if (mList != null) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public ShopViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ShopViewHolder(mInflater.inflate(R.layout.item_collect_shop_recycler, parent, false));
    }

    @Override
    public void onBindViewHolder(ShopViewHolder holder, int position) {
        final CollentShopEntity.ListBean listBean = mList.get(position);

        //门店图片
        GlideUtils.showImageView(mContext, R.mipmap.ic_serve_default, listBean.getLogoUrl(), holder.itemIvImgAppintment);
        //门店名称
        holder.itemTvNameAppintment.setText(listBean.getName());
        //门店地址
        holder.itemTvDetails.setText(listBean.getAddress());
        //门店距离
//        holder.itemTvDistance.setText(listBean.getDistance()+ "km");

        //item点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemListener.onItemClick(listBean.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    static class ShopViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_iv_img_appintment)
        ImageView itemIvImgAppintment;
        @BindView(R.id.item_tv_name_appintment)
        TextView itemTvNameAppintment;
        @BindView(R.id.item_tv_details)
        TextView itemTvDetails;
        @BindView(R.id.item_tv_distance)
        TextView itemTvDistance;
        public ShopViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(String id);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemListener = listener;
    }
}
